<?php
namespace App\Services\Admin;

use App\Repositories\Admin\CompanyRepository;
use App\Repositories\Admin\JobCompanyRepository;
use App\Repositories\Admin\JobRepository;
use App\Repositories\Admin\OrderBaleRepository;
use App\Repositories\Admin\SearchRepository;
use App\Repositories\Admin\SearchLogRepository;
use App\Repositories\Admin\StaffRepository;
use App\Repositories\Admin\UserRepository as User;
use App\Repositories\UserRepository;

use App\Models\Admin\CompanyModel;
use App\Models\AreaModel;
use App\Models\Admin\CategoryModel;
use App\Models\Admin\StaffModel;
use App\Models\Admin\OrderBaleModel;

class SearchService
{
    private $service;
    private $log;
    private $user;
    private $user2;
    private $com;
    private $staff;
    private $bale;
    private $job;

    public function __construct()
    {
        $this->service = new SearchRepository();
        $this->log = new SearchLogRepository();
        $this->user = new UserRepository();
        $this->user2 = new User();
        $this->com = new CompanyRepository();
        $this->staff = new StaffRepository();
        $this->bale = new OrderBaleRepository();
        $this->job = new JobRepository();
    }

    public function getIndex($word)
    {
        $models = $this->service->getIndex($word);
        if (!count($models)) { return ''; }
        $staffs = array();
        $coms = array();
        foreach ($models as $k=>$model) {
            $type = $model->type;
//            $typeName = $this->service->getTypeName($model->type);
            if ($model->type==3) {
                $staff = $this->staff->getShowOne($model->from);
                $uid = $staff ? $staff->uid : 0;
                $user = $this->user->getUserById($uid);
                $staff['type'] = $type;
                $staff['head'] = $user ? $user->head : '';
                $staff['img'] = $user ? $user->head : '';
                $staff['mobile'] = $user ? $user->phone : '';
                $staff['uname'] = $user ? $user->name : '测试用户';
                $staffs[] = $staff;
            } else {
                $com = $this->com->getShowOne($model->from);
                $uid = $com ? $com->uid : 0;
                $user = $this->user->getUserById($uid);
                $company['type'] = $type;
                $company['img'] = $user ? $user->head : '';
                $company['uname'] = $user ? $user->name : '测试用户';
                $company['intro'] = $com ? $com->detail : '测试简介';
                $coms[] = $company;
            }
        }
        return array(
            'staffs' => $staffs,
            'coms' => $coms,
        );
    }

    public function getApiIndex($word,$type)
    {
        $models = $this->service->getApiIndex($word,$type);
        if (!count($models)) { return []; }
        $staffs = array();
        $coms = array();
        $bales = array();
        foreach ($models as $k=>$model) {
            $type = $model->type;
            if ($model->type==3) { //工人
                $staffModel = $this->staff->getShowOne($model->from);
                $uid = $staffModel ? $staffModel->uid : 0;
                $user = $this->user->getUserById($uid);
                $staff['type'] = $type;
                $staff['head'] = $user ? $user->head : '';
                $staff['img'] = $user ? $user->head : '';
                $staff['mobile'] = $user ? $user->phone : '';
                $staff['uname'] = $user ? $user->name : '测试用户';
                $staffs[] = $staff;
            } else if ($model->type==4) { //货单
                $baleModel = $this->bale->getShowOne($model->from);
                $uid = $baleModel ? $baleModel->uid : 0;
                $user = $this->user->getUserById($uid);
                $bale['img'] = $user ? $user->head : '';
                $bale['uname'] = $user ? $user->name : '测试用户';
                $bales[] = $bale;
            } else { //企业
                $com = $this->com->getShowOne($model->from);
                $uid = $com ? $com->uid : 0;
                $user = $this->user->getUserById($uid);
                $company['type'] = $type;
                $company['img'] = $user ? $user->head : '';
                $company['uname'] = $user ? $user->name : '测试用户';
                $company['intro'] = $com ? $com->detail : '测试简介';
                $coms[] = $company;
            }
        }
        return array(
            'staffs' => $staffs,
            'coms' => $coms,
            'bales' => $bales,
        );
    }

    public function add($data)
    {
        return $this->service->setAdd($data);
    }

    public function getOne($id)
    {
        return $this->service->getShowOne($id);
    }

    public function getIndexLogList($uid)
    {
        return $this->log->getIndexs($uid);
    }

    public function getApiLogList($uid)
    {
        return $this->log->getApiIndexs($uid);
    }

    /**
     * 设置用户搜索记录
     */
    public function setApiLog($uid,$word)
    {
        return $this->log->setApiAdd($uid,$word);
    }

    /**
     * 初始化搜索内容
     * 公司、工厂、工人、货单
     */
    public function setInit()
    {
        $comModels = CompanyModel::all();
        //公司工厂
        $resComs = $this->getComFields($comModels);
        //工人资料
        $resStaffs = $this->getStaffFields();
        //货单
        $resBales = $this->getBaleFields();
        //岗位
        $resJobs = $this->getJobFields();
        return array(
            'coms' => $resComs['coms'],
            'facs' => $resComs['facs'],
            'staffs' => $resStaffs,
            'bales' => $resBales,
            'jobs' => $resJobs,
        );
    }

    /**
     * 获取公司工厂资料
     * 用于搜索的字段
     * cate、com_name、com_address、com_city、out_type、
     * products、styles、pro_types、designs、equips
     */
    public function getComFields($comModels)
    {
        $coms = array();
        $facs = array();
        if (count($comModels)) {
            foreach ($comModels as $comModel) {
                $company['name'] = $comModel->com_name;
                $company['address'] = $comModel->com_address;
                //城市
                if ($cityId=$comModel->com_city) {
                    $areaModel = AreaModel::where('id',$cityId)
                        ->first();
                    $company['cityName'] = $areaModel ?
                        $areaModel->name : 0;
                } else {
                    $company['cityName'] = 0;
                }
                //业务类型
                if ($outTypeId=$comModel->out_type) {
                    $cateModel = CategoryModel::where('id',$outTypeId)
                        ->first();
                    $company['outTypeName'] = $cateModel ?
                        $cateModel->name : 0;
                } else {
                    $company['outTypeName'] = 0;
                }
//                //产品
//                if ($pros=$comModel->products) {
//                }
//                //风格
//                if ($styles=$comModel->styles) {
//                }
//                //产品类型
//                if ($proTypes=$comModel->pro_types) {
//                }
//                //款式
//                if ($designs=$comModel->designs) {
//                }
//                //设备
//                if ($equips=$comModel->equips) {
//                }
                if ($comModel->cate==1) {
                    $company['cateName'] = '工厂';
                    $facs[$comModel->uid] = implode('||',$company);
                } else {
                    $company['cateName'] = '公司';
                    $coms[$comModel->uid] = implode('||',$company);
                }
            }
        }
        return array(
            'coms' => $coms,
            'facs' => $facs,
        );
    }

    /**
     * 获取员工资料
     * 用于搜索的字段
     * cate、education、household_address、residential_address、
     * skill、hobby、job、
     */
    public function getStaffFields()
    {
        $staffModels = StaffModel::all();
        $staffs = array();
        if (count($staffModels)) {
            foreach ($staffModels as $staffModel) {
                //员工类型
                if ($staffModel->cate) {
                    $staff['cateName'] = $staffModel->cate==1 ?
                        '工厂员工' : '公司员工';
                } else {
                    $staff['cateName'] = 0;
                }
                //学历
                if ($edu=$staffModel->education) {
                    $staffModel0 = new StaffModel();
                    $edus = $staffModel0->getEdus();
                    $staff['eduName'] = array_key_exists($edu,$edus) ?
                        $edus[$edu] : 0;
                } else {
                    $staff['eduName'] = 0;
                }
                //户口地址
                $staff['household'] = $staffModel->household_address;
                //居住地址
                $staff['address'] = $staffModel->residential_address;
                //专业技能
                if ($skillStr=$staffModel->skill) {
                    $skillsNew = array();
                    $skills = explode(',',$skillStr);
                    foreach ($skills as $skill) {
                        $cateModel = CategoryModel::where('id',$skill)->first();
                        $skillsNew[] = $cateModel ? $cateModel->name : '';
                    }
                    $staff['skillName'] = $skillsNew ?
                        implode(',',$skillsNew) : 0;
                } else {
                    $staff['skillName'] = 0;
                }
                //兴趣爱好
                if ($hobbyStr=$staffModel->hobby) {
                    $hobbysNew = array();
                    $hobbys = explode(',',$hobbyStr);
                    foreach ($hobbys as $hobby) {
                        $cateModel = CategoryModel::where('id',$hobby)->first();
                        $cateModel = CategoryModel::where('id',$hobby)->first();
                        $hobbysNew[] = $cateModel ? $cateModel->name : '';
                    }
                    $staff['hobbyName'] = $hobbysNew ?
                        implode(',',$hobbysNew) : 0;
                } else {
                    $staff['hobbyName'] = 0;
                }
                //岗位
                if ($job=$staffModel->job) {
                    $cateModel = CategoryModel::where('id',$job)->first();
                    $staff['jobName'] = $cateModel ? $cateModel->name : 0;
                } else {
                    $staff['jobName'] = 0;
                }
                $staffs[$staffModel->uid] = implode('||',$staff);
            }
        }
        return $staffs;
    }

    /**
     * 获取货单资料
     * 用于搜索的字段
     * cate、proType、region、proWay、stuff、
     * deplete、orderType、style、design、
     */
    public function getBaleFields()
    {
        $baleModels = OrderBaleModel::all();
        $bales = array();
        if (count($baleModels)) {
            foreach ($baleModels as $baleModel) {
                //货单类型
                if ($cateId=$baleModel->cate) {
                    $cateModel = CategoryModel::where('id',$cateId)
                        ->first();
                    $bale['cateName'] = $cateModel ?
                        $cateModel->name : 0;
                } else {
                    $bale['cateName'] = 0;
                }
                //外发要求
                $bale['proTypeName'] = $baleModel->proType==1 ?
                    '本地生产' : '外地生产';
                //地区选择
                if ($areaId=$baleModel->region) {
                    $areaModel = AreaModel::where('id',$areaId)
                        ->first();
                    $bale['regionName'] = $areaModel ?
                        $areaModel->name : 0;
                } else {
                    $bale['regionName'] = 0;
                }
                //生产要求
                $bale['proWayName'] = $baleModel->proWay==1 ?
                    '流水做' : '整件做';
//                //面辅料成分
//                if ($stuff=$baleModel->stuff) {
//                }
//                //面辅料单耗
//                if ($deplete=$baleModel->deplete) {
//                }
//                //产品类型
//                if ($orderType=$baleModel->orderType) {
//                }
//                //产品风格
//                if ($style=$baleModel->style) {
//                }
//                //产品款式
//                if ($design=$baleModel->design) {
//                }
                $bales[$baleModel->id] = implode('||',$bale);
            }
        }
        return $bales;
    }

    /**
     * 获取岗位资料
     * 用于搜索的字段
     * cname、jobName、emolument、welfare、rest、
     * address、jobName、emolument、welfare、rest、
     * ageName、skill、
     */
    public function getJobFields()
    {
        $models = JobCompanyRepository::where('del',0)->get();
        $jobs = array();
        if (count($models)) {
            foreach ($models as $model) {
                $jobCom = array();
                //公司名称
                if ($uid=$model->uid) {
                    $com = $this->com->getShowOne($uid);
                    $jobCom['cname'] = $com ? $com->com_name : '';
                } else {
                    $jobCom['cname'] = '';
                }
                //岗位名称
                if ($job=$model->job) {
                    $jobModel = $this->job->getShowOne($job);
                    $jobCom['jobName'] = $jobModel ? $jobModel->name : '';
                } else {
                    $jobCom['jobName'] = '';
                }
                $jobCom['emolument'] = $model->emolument;
                $jobCom['welfare'] = $model->welfare;
                $jobCom['rest'] = $model->rest;
                $jobCom['address'] = $model->address;
                $ages = $model->age_range;
                $jobCom['ageName'] = '年龄范围：'.$ages[0].'到'.$ages[1];
                $jobs[$model->id] = implode('||',$jobCom);
            }
        }
        return $jobs;
    }

    /**
     * 设置公司工厂资料
     * 用于搜索的字段
     * cate、com_name、com_address、com_city、out_type、
     * @param $cid 公司的UID
     * @param $params 更新参数键值对
     *              cateName 企业类型
     *              name 企业名称
     *              address 地址
     *              cityName 城市名称
     *              outTypeName 业务类型
     */
    public function setComFields($cid,$params)
    {
        $fields = array(
            'cateName', 'name', 'address', 'cityName', 'outTypeName',
        );
        $model = $this->com->getShowOneByUid($cid);
        if (!$model || !$params) {return false; }
        foreach ($params as $key => $param) {
            if (!in_array($key,$fields)) { return false; }
        }
        $type = $model->cate==1 ? 2 : 1; //1工厂，2公司
        $paramStr = implode('||||',$params);
        $this->service->setModifyByType($type,$cid,$paramStr);
        return true;
    }

    /**
     * 设置员工资料
     * 用于搜索的字段
     * cate、education、household_address、residential_address、
     * skill、hobby、job、
     * @param $uid 员工的UID
     * @param $params 更新参数键值对
     *              cateName 员工类型
     *              eduName 学历名称
     *              household 户口地址
     *              address 居住地址
     *              skillName 技能拼接字符串
     *              hobbyName 兴趣拼接字符串
     *              jobName 岗位名称
     */
    public function setStaffFields($uid,$params)
    {
        $fields = array(
            'cateName', 'eduName', 'household', 'address',
            'skillName', 'hobbyName', 'jobName',
        );
        $model = $this->staff->getShowOneByUid($uid);
        if (!$model || !$params) {return false; }
        foreach ($params as $key => $param) {
            if (!in_array($key,$fields)) { return false; }
        }
        $paramStr = implode('||||',$params);
        $this->service->setModifyByType(3,$uid,$paramStr);
        return true;
    }

    /**
     * 设置货单资料
     * 用于搜索的字段
     * cate、proType、region、proWay、
     * @param $baleId 货单的ID
     * @param $params 更新参数键值对
     *              cateName 货单类型
     *              proTypeName 外发要求
     *              regionName 外发范围城市
     *              proWayName 生产要求名称
     */
    public function setBaleFields($baleId,$params)
    {
        $fields = array(
            'cateName', 'proTypeName', 'regionName', 'proWayName',
        );
        $model = $this->staff->getShowOneByUid($baleId);
        if (!$model || !$params) {return false; }
        foreach ($params as $key => $param) {
            if (!in_array($key,$fields)) { return false; }
        }
        $paramStr = implode('||||',$params);
        $this->service->setModifyByType(4,$baleId,$paramStr);
        return true;
    }
}